package com.caigou.modules.gallery.ui.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.gallery.R
import com.caigou.modules.gallery.adpter.GalleryItemAdapter
import com.caigou.modules.gallery.bean.GalleryBean
import com.caigou.modules.gallery.bean.GalleryItemBean
import com.caigou.modules.gallery.databinding.FragmentCollectBinding
import com.caigou.modules.gallery.ui.GalleryActivity
import com.caigou.modules.gallery.viewmodel.GalleryViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/01
 */
@AndroidEntryPoint
class CollectFragment : BaseFragment() {

    private var _binding: FragmentCollectBinding? = null
    private val binding get() = _binding!!

    private val galleryViewModel: GalleryViewModel by viewModels()

    private val mAdapter: GalleryItemAdapter by lazy {
        GalleryItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentCollectBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.collectSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchCollectList()
            }
        }
        binding.collectRecyclerView.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val bean = mAdapter.getItem(position)
            val type = (requireActivity() as GalleryActivity).type
            ARouter.getInstance().build(RouterPath.Gallery.PAGE_GALLERY_DETAIL)
                .withInt(AppConstants.DATA_TYPE, type)
                .withInt(AppConstants.DEPOT_ID, bean.depotId)
                .withString(AppConstants.DEPOT_NAME, bean.depotName)
                .navigation()
        }

    }

    override fun lazyLoad() {
        fetchCollectList()
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(galleryViewModel.collectListLiveData, ::handleGetCollectList)
    }

    private fun handleGetCollectList(result: Resource<MutableList<GalleryBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.collectSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取帖子详细信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.collectSwipeRefreshLayout.isRefreshing = false
                val items = mutableListOf<GalleryItemBean>()
                result.data?.onEach {
                    items.addAll(it.list)
                }
                mAdapter.setList(items)
            }
        }
    }

    fun fetchCollectList() {
        if (AppBackgroundInfo.instance.hasLogin) {
            val userId = AppBackgroundInfo.instance.userBean!!.userId
            val type = (requireActivity() as GalleryActivity).type
            galleryViewModel.getCollectList(type, userId)
        } else {
            showToast("请您先登录！")
        }

    }
}